package org.jeecg.modules.biz;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.jeecg.modules.biz.entity.Chemicals;
import org.jeecg.modules.biz.service.IChemicalsService;
import org.jeecg.modules.online.cgform.enhance.CgformEnhanceJavaInter;
import org.jeecg.modules.online.config.exception.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component("ChOrderInter")
public class ChOrderInter implements CgformEnhanceJavaInter {
    @Autowired
    private IChemicalsService service;

    @Override
    public void execute(String s, JSONObject jsonObject) throws BusinessException {
        String goodsId = jsonObject.getString("goods_id");
        Integer exwNum = jsonObject.getInteger("exw_num");

        LambdaQueryWrapper<Chemicals> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Chemicals::getGoodsId, goodsId);
        Chemicals chemicals = service.getOne(queryWrapper);
        if (chemicals.getGoodsNum() - exwNum < 0) {
            throw new BusinessException("库存不足！实际库存：" + chemicals.getGoodsNum() + ", 提取数量：" + exwNum);
        } else {
            LambdaUpdateWrapper<Chemicals> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(Chemicals::getGoodsId, goodsId);
            updateWrapper.set(Chemicals::getGoodsNum, chemicals.getGoodsNum() - exwNum);
            service.update(updateWrapper);
        }
    }
}
